<?php defined('SYSPATH') OR die('No direct access allowed.');

class Channel_Controller extends Master_Channel_Controller{

	//list channel by owner
	public function index(){
		Kohana::log("debug","channel/channel -> Channel_Controller(index)");

		$view = new View('channel/channel');

		$this->set_title("สมาชิก - สถานีเพลง");

		//get user
		$auth = new Auth();
		$user_id = $auth->get_user()->id;

		$channels = new Channels_Model;

		// Instantiate Pagination, passing it the total number of product rows.
		$paging = new Pagination(array
		(
			'base_url'    => 'channel/channel/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $channels->count_channels(),
		));

		// Render the page links
		$view->pagination = $paging->render();

		// Display X items per page, from offset = page number
		$view->channels = $channels->get_channels_user($user_id);

		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	//ajax
	public function ajax_page($pagenum=1)
	{
		Kohana::log("debug","Function ajax_page");
		if(request::is_ajax())
		{
			$this->auto_render=false;
			$ajax = new ISS_Ajax();

			$view = new View('channel/channel');
			$channels = new Channels_Model;

			// Instantiate Pagination, passing it the total number of product rows.
			$paging = new Pagination(array
			(
				'base_url'    => 'channel/channel/ajax_page/',
				'uri_segment' => 4,
				'total_items' => $channels->count_channels(),
			));

			// Render the page links
			$view->pagination = $paging->render();

			// Display X items per page, from offset = page number
			// $view->channels = $channels->browse($paging->items_per_page, $paging->sql_offset);
			$auth = new Auth;
			$user_id = $auth->get_user()->id;
			$view->channels = $channels->get_channels_user($user_id);

			$selector = "mainCont";
			$content = $view;
			$ajax->addViewUpdate($selector, $content);
			echo json_encode($ajax->getHtmlAjax());
		}
		else {
			$this->page($pagenum);
		}
	}

	public function page($pagenum=1)
	{
		Kohana::log("debug","Function Page");

		//		$auth = new Auth;
		//		$user_role = $auth->logged_in('admin');
		//		Kohana::log('info',$user_role);

		//		if ($user_role) {
		$view = new View('channel/channel');
		//		}else{
		//			$view = new View('channel/index');
		//		}

		$this->set_title('สมาชิก - สถานีเพลง');
			
		$channels = new Channels_Model;

		// Instantiate Pagination, passing it the total number of product rows.
		$paging = new Pagination(array
		(
			'base_url'    => 'channel/channel/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $channels->count_channels(),
		));

		// Render the page links
		$view->pagination = $paging->render();

		// Display X items per page, from offset = page number
		//$view->channels = $channels->browse($paging->items_per_page, $paging->sql_offset);
		$view->channels = $channels->loads();

		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	public function add($status='') {
		$view = new View('channel/channel_user_form');

		$this->set_title('สร้างสถานีเพลง');

		$form = array (
				'id' => '',
				'title'    => '',
				'description'   => '',
				'logo' => '',
				'playlists'=>'',
		);

		$playlists = new Channels_Playlists_Model;
		$auth = new Auth;
		$user_id = $auth->get_user()->id;


		$view->mode = 'add';
		$view->status = $status;
		if ($status != '') {
			$form['title'] = (isset($_POST['txtName']) ? $_POST['txtName'] : '');
			$form['description'] = (isset($_POST['txtDescription']) ? $_POST['txtDescription'] : '');
			$form['logo'] = (isset($_POST['txtLogo']) ? $_POST['txtLogo'] : '');
		}
		$view->form = $form;
		//		$pl = $playlists->get_playlists_user($user_id);
		//		$view->playlists = $playlists->get_playlists_user($user_id);
		$view->playlists = $playlists->get_playlists();

		//		Kohana::log("debug","CCCCC  ".count($pl));
		//
		//		foreach ($pl as $variable) {
		//			Kohana::log("debug","CCCCC  ".$variable['id']);;
		//		}

		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	//edit
	public function edit($id, $status=''){
		Kohana::log("debug","c->channel->channel->function edit");

		$view = new View("channel/channel_user_form");

		$this->set_title('สมาชิก - แก้ไขสถานีเพลง');
		$channels = new Channels_Model;

		$view->mode = 'edit';
		$view->status = $status;

		$playlists = new Channels_Playlists_Model;
		$auth = new Auth;
		$user_id = $auth->get_user()->id;

		if ($status != '') {
			$form['id'] = (isset($_POST['id']) ? $_POST['id'] : '');
			$form['title'] = (isset($_POST['txtName']) ? $_POST['txtName'] : '');
			$form['description'] = (isset($_POST['txtDescription']) ? $_POST['txtDescription'] : '');
			$form['logo'] = $channels->get_image_fileneme($id);
			$view->form = $form;
		}
		else {

			$forms = $channels->load($id);

			foreach ($forms as $row)
			{
				$view->form = $row;
			}
		}

		$view->errors = array();
		//		$view->playlists = $playlists->get_playlists();
		//		$view->selected_playlist = $playlists->get_selected_playlist($user_id,$id);
		$view->playlists = $playlists->get_playlist_in_channel($user_id,$id);
		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	public function del($id) {
		Kohana::log("debug","c->channel->channel->function del");
		if(request::is_ajax())
		{
			$this->auto_render=false;
			$ajax = new ISS_Ajax();

			$channels = new Channels_Model;
			if ($channels->delete($id)) {
				$this->ajax_page(1);
			}
			else {
				$tag = "alert";
				$script = "alert('". Kohana::lang('channel_error.save_result.delete_failed') ."')";
				$ajax->addExecJScript($tag, $script);
				echo json_encode($ajax->getHtmlAjax());
			}
		}
		else {
			$this->page(1);
		}
	}

	public function save(){
		Kohana::log("debug","c->channel->channel->function save");
		if($_POST) {

			//get status user
			$auth = new Auth;

			if ( $auth->logged_in()) {
				$user_id = $auth->get_user()->id;
			}

			$post = new Validation($_POST);
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txtName','required', 'length[2,255]');
			$post->post_filter('ucfirst', 'title');

			//			foreach ($_POST['chbPl'] as $rpl) {
			//				Kohana::log("debug",$rpl);
			//			}

			$chkPl=null;
			if (!empty($_POST['chbPl'])) {
				$chkPl = $_POST['chbPl'];
				Kohana::log("debug","count chk ".count($chkPl));
			}

			$form = array (
        		'id' => '',
				'title' => '',
				'description' => '',
				'logo' => '',
				'playlists' => '',
			);

			if (!$post->validate())
			{
				$errors = $post->errors('channel_error');
				$view = new View('channel/channel_user_form');

				$view->errors = $errors;
				$form['title'] = (isset($_POST['txtName']) ? $_POST['txtName'] : '');
				$form['description'] = (isset($_POST['txtDescription']) ? $_POST['txtDescription'] : '');
				$form['logo'] = (isset($_POST['txtLogo']) ? $_POST['txtLogo'] : '');
				//				$form['playlists'] = (isset($_POST['chbPl']) ? $_POST['chbPl'] : '');
				$view->form = $form;
				$this->set_title('สมาชิก - สร้างสถานีเพลง');
				$view->mode = 'add';
				$this->template->content = $view;
				$this->template->render(TRUE);
			}
			else {

				$form['ownerId'] = $user_id;

				$channels = new Channels_Model;
				$channels_palylist = new Channels_Playlists_Model;

				$channels->file_validate('txtLogo');

				if ($_POST['mode']=='edit') {
					$form['id'] = $_POST['id'];
				}

				$form['title'] = $_POST['txtName'];
				$form['description'] = $_POST['txtDescription'];
				$filename = $channels->get_filename();

				if (!empty($filename)) {
					$form['logo'] = str_replace(DOCROOT, '', $channels->get_filename());
				}
				//				Kohana::log("debug",$form['logo']);

				if ($channels->save2($form,$chkPl, $_POST['mode'])) {
					url::redirect('channel/channel');
				}
				else {
					$status = Kohana::lang('channel_error.save_result.save_failed');
					if ($_POST['mode']=='add') {
						$this->add($status);
					}
					else {
						$this->edit($_POST['id'], $status);
					}
				}
			}
		}
	}

}